﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Pasta.Medio.Concrete;
using Pasta.Medio.Configuration;

namespace Pasta.Medio.Controls
{
	public partial class SettingsTab : TabBase
	{
		private AppSettings _settings;
		private IEnumerable<Pasta.Medio.Domain.Enum> _cameras;

		public SettingsTab()
		{
			_settings = Program.Settings;

			InitializeComponent();

			cameraGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
			camName.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			cameraGrid.AutoGenerateColumns = false;
		}

		public override void RefreshPage()
		{
			tbImageFolder.Text = _settings.ImageFolder;
			tbImageTypes.Text = _settings.ImageTypesString;
			tbVideoFolder.Text = _settings.VideoFolder;
			tbVideoTypes.Text = _settings.VideoTypesString;
			tbRenamePattern.Text = _settings.RenamePattern;
			tbDuplicatePattern.Text = _settings.DuplicatePattern;

			_cameras = Program.Enums.Find(cam => cam.Type == "Camera");
			cameraGrid.DataSource = _cameras.ToList();
		}

		private void UpdateSettings(object sender, EventArgs e)
		{
			_settings.ImageFolder = tbImageFolder.Text;
			_settings.ImageTypes = AppSettings.ReadTypes(tbImageTypes.Text);
			_settings.VideoFolder = tbVideoFolder.Text;
			_settings.VideoTypes = AppSettings.ReadTypes(tbVideoTypes.Text);
			_settings.RenamePattern = tbRenamePattern.Text;
			_settings.DuplicatePattern = tbDuplicatePattern.Text;
			_settings.Save();
		}

		private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex >= 0)
			{
				Program.Enums.SaveChanges();
			}
		}
	}
}
